package com.zhl.dao;

import com.zhl.dao.dasedao.BaseDao;
import com.zhl.model.Dept;
import com.zhl.utils.JdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Struct;
import java.util.List;
import java.util.Map;

public class DeptDao extends BaseDao<Dept> {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());

    public DeptDao() {
        super(Dept.class);
    }

    public List<Dept> fingAll(){
        String sql = "select * from dept";
        List<Dept> depts = null;
        try {
            depts = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Dept.class));
        }catch (Exception e){
            return null;
        }
        return depts;
    }
    public Dept findByDept(String DeptName){
        String sql = "select * from dept where name = ?";
        Dept dept = null;
        try {
            dept = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Dept.class),DeptName);
        }catch (Exception e){
            return null;
        }
        return dept;
    }

    public List<Map<String, Object>> findDeptInfo(){
        List<Map<String, Object>> deptList = null;
        String sql = "select d.*,u.id as \"uId\",u.username,u.real_name from dept d " +
                "left join user u on d.id = u.dept_id;";
        try {
            deptList = jdbcTemplate.queryForList(sql);
        }catch (Exception e){
            return null;
        }
        return deptList;
    }

}
